#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<string>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* active(void* args)
{
    std::string name= static_cast<const char*>(args);
    while(true)
    {
        pthread_mutex_lock(&mutex);

        pthread_cond_wait(&cond,&mutex);//自动解锁等待，被唤醒后，会自动上一个新锁
        std::cout<<name<<std::endl;
        pthread_mutex_unlock(&mutex);
        
    }


}

int main()
{ pthread_t tid1, tid2, tid3;
    pthread_create(&tid1, nullptr, active, (void *)"thread-1");
    pthread_create(&tid2, nullptr, active, (void *)"thread-2");
    pthread_create(&tid3, nullptr, active, (void *)"thread-3");

    sleep(1);
    printf("Main thread ctrl begin...\n");

    while (true)
    {
        printf("main wakeup thread...\n");
        pthread_cond_signal(&cond);
        //pthread_cond_broadcast(&cond);
        sleep(1);
    }

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);
    pthread_join(tid3, nullptr);
}